package com.baosight.risk.web.rire;

import com.baosight.risk.api.rire.RiwaEquityInvestService;
import com.baosight.risk.base.controller.BaseController;
import com.baosight.risk.common.result.AjaxResult;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.mapper.riwa.RiwaEquityInvestMainMapper;
import com.baosight.risk.service.riwa.entity.RiwaEquityInvest;
import com.baosight.risk.service.riwa.entity.RiwaEquityInvestMain;
import com.baosight.risk.service.riwa.vo.RiwaEquityInvestVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;

@Slf4j
@RestController
@RequestMapping("/riwa/equity/invest")
@Api(value = "长期股权投资收益预警模型", description = "长期股权投资收益预警模型", produces = MediaType.APPLICATION_JSON)
public class RiwaEquityInvestController extends BaseController {

    @Autowired
    private RiwaEquityInvestService equityInvestService;
    @Autowired
    private RiwaEquityInvestMainMapper investMainMapper;

    @PostMapping("/selectList")
    @ApiOperation("列表查询")
    public AjaxResult selectList(@RequestBody RiwaEquityInvestMain equityInvestMain) {
        startPage();
        return getResultPage(equityInvestService.selectList(equityInvestMain));

    }

    @GetMapping("/getById")
    @ApiOperation("获取详情")
    public AjaxResult getById(@RequestParam("id") String id) {
        if (StringUtils.isEmpty(id)) {
            log.error("数据为空！");
            return error("数据为空！");
        }
        return success(equityInvestService.getById(id));

    }

    @GetMapping("/selectLastQuarter")
    @ApiOperation("查询上一季度投资单位名称")
    public AjaxResult selectLastQuarter(@RequestParam("acctYear") String acctYear, @RequestParam("quarter") String quarter) {
        RiwaEquityInvestMain riwaEquityInvestMain = investMainMapper.selectQuarter(acctYear, quarter);
        if (riwaEquityInvestMain != null) {
            return error("当前季度已存在数据");
        }
        return success(equityInvestService.selectLastQuarter(acctYear, quarter));

    }

    @GetMapping("/getCustomizeModel")
    @ApiOperation("获取自定义模型")
    public AjaxResult getCustomizeModel(RiwaEquityInvestMain main) {
        return AjaxResult.success("查询成功", equityInvestService.selectByYearAndQuarter(main));
    }

    @PostMapping("/saveEquityInvest")
    @ApiOperation("保存")
    public AjaxResult saveEquityInvest(@RequestBody RiwaEquityInvestVO equityInvestVO) throws Exception {
        return success(equityInvestService.saveEquityInvest(equityInvestVO));
    }

    @PostMapping("/save")
    @ApiOperation("保存明细")
    public AjaxResult save(@RequestBody RiwaEquityInvestMain equityInvestMain) throws Exception {
        return success(equityInvestService.save(equityInvestMain));
    }


}
